﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Radiation___IM
{
    public class ClientRegister
    {
        private Hashtable m_clients;
        private Int64 m_key;

        public ClientRegister()
        {
            m_clients = new Hashtable();
        }

        public Client get(Int64 key)
        {
            try
            {
                if (m_clients.ContainsKey(key))
                    return (Client)m_clients[key];
                else
                    return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public Int64 register(Client client)
        {
            try
            {
                Int64 key;

                lock (m_clients)
                {
                    key = m_key;
                    m_key++;
                    m_clients.Add(key, client);
                }

                return key;
            }
            catch (Exception)
            {
                return (Int64)rvalue.ERROR_OCCURED_DURING_PROCESSING;
            }
        }

        public rvalue unRegister(Int64 key)
        {
            try
            {
                if (m_clients.ContainsKey(key))
                {
                    m_clients.Remove(key);
                    return rvalue.CLIENT_UNREGISTERED;
                }
                else
                    return rvalue.CLIENT_DOES_NOT_EXIST;
            }
            catch (Exception)
            {
                return rvalue.ERROR_OCCURED_DURING_PROCESSING;
            }
        }
    }
}
